/*******************************************************************************
 * Copyright (c) 2007 University of Oslo.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     University of Oslo - initial API and implementation
 *******************************************************************************/

package com.cea.papyrus.diagram.sequence.sedi.edit.command;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.gef.EditPart;
import org.eclipse.gef.commands.Command;
import org.eclipse.uml2.uml.Interaction;
import org.eclipse.uml2.uml.InteractionFragment;
import org.eclipse.uml2.uml.Lifeline;

import com.cea.papyrus.core.utils.LookForElement;
import com.cea.papyrus.diagram.sequence.sedi.edit.analysis.CoverableAnalyser;
import com.cea.papyrus.diagram.sequence.sedi.model.util.ModelUtil;
import com.cea.papyrus.diagraminterchange2.di2.GraphNode;

/**
 * Performs geometrical analysis of a diagram in order to detect lifeline covering by
 * interaction fragments.
 * <p>
 * The following attributes must be set before the command can run successfully:
 * </p>
 * <ul>
 * <li>LifelineView
 * <li>EditPart
 * </ul>
 * <p>
 * When completed, the following attributes can be read to access the result:
 * </p>
 * <ul>
 * <li>CoveredBy
 * </ul>
 * 
 * @author frankd@ifi.uio.no
 */
public class AnalyseCoveredByCommand extends Command {

    private EditPart anEditPart;
    private GraphNode lifelineView;
    private List<InteractionFragment> coveredBy;

    public void setLifelineView(final GraphNode lifelineView) {
        this.lifelineView = lifelineView;
    }

    public void setEditPart(final EditPart editPart) {
        this.anEditPart = editPart.getRoot();
    }

    public List<InteractionFragment> getCoveredBy() {
        return coveredBy;
    }

    @SuppressWarnings("unchecked")
    @Override
    public void execute() {
        final Interaction interaction =
                ModelUtil.<Lifeline> getSemantic(lifelineView).getInteraction();
        ArrayList<GraphNode> listInter =
                LookForElement.lookForGraphElementWODiagram(interaction, LookForElement
                        .getDiagram(lifelineView), new ArrayList<GraphNode>(1));
        final GraphNode interactionView = listInter.get(0);
        final CoverableAnalyser analyser = new CoverableAnalyser(anEditPart, interactionView);

        coveredBy = analyser.findCoveredBy(lifelineView);
    }

    @Override
    public void undo() {
        coveredBy = null;
    }

    @Override
    public void redo() {
        execute();
    }

    @Override
    public void dispose() {
        lifelineView = null;
        coveredBy = null;
        anEditPart = null;
    }
}
